package com.ransj.ndkpath.basic;

import java.io.IOException;
import java.util.MissingFormatArgumentException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.nfc.tech.MifareClassic;
import android.util.Log;
import android.widget.Toast;

import com.ransj.ndkpath.common.ChildActivity;

public class PageMethod extends ChildActivity {
	private static final String TAG = PageMethod.class.getSimpleName();
	private static final String[] NAME = { //
			"SUM",//
			"GET ARRAY LENGTH",//
			"PLUS",//
			"LOAD SOUND",//
			"PLAY SOUND",//
			"STOP SOUND",//
			"DEFAULT CONSTRUCTOR",//
			"CONSTRUCTOR WITH NAME",//
	};
	
	private MediaPlayer mPlayer;
	private boolean mIsMusicOk;

	@Override
	protected void initData() {
		this.mNameArray = NAME;
	}

	@Override
	protected void onItemClick(int index) {
		switch (index) {
		case 0:
			sum();
			break;
		case 1:
			getArrayLength();
			break;
		case 2:
			plus();
			break;
		case 3:
			load();
			break;
		case 4:
			play();
			break;
		case 5:
			stop();
			break;
		case 6:
			defaultConstructor();
			break;
		case 7:
			constructorWithName("NDK");
			break;
		}
	}
	
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		stop();
	}

	private int sum(int[] array) {
		int sum = 0;
		for (int i = 0; i < array.length; i++) {
			sum += array[i];
		}
		return sum;
	}

	private int getArrayLength(int[] array) {
		return array.length;
	}

	private static int plus(int[] array) {
		int plus = 1;
		for (int i = 0; i < array.length; i++) {
			plus *= array[i];
		}
		return plus;
	}
	
	private void loadSound(String path){
		if(mPlayer == null){
			try {
				mPlayer = new MediaPlayer();
				mPlayer.setDataSource(getAssets().openFd(path).getFileDescriptor());
				mPlayer.setOnPreparedListener(new OnPreparedListener() {
					
					@Override
					public void onPrepared(MediaPlayer mp) {
						Toast.makeText(PageMethod.this, "Music is OK !", Toast.LENGTH_SHORT).show();
						mIsMusicOk= true;
					}
				});
				mPlayer.prepare();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void playSound() {
		if(mIsMusicOk){
			mPlayer.start();
		}else{
			Toast.makeText(this, "Please load sound first", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void stopSound(){
		if(mPlayer != null){
			mIsMusicOk = false;
			mPlayer.release();
			mPlayer = null;
		}
	}

	private native void sum();

	private native void getArrayLength();

	private native void plus();
	
	private native void load();

	private native void play();
	
	private native void stop();
	
	private native void defaultConstructor();
	
	private native void constructorWithName(String name);
	
}

class Method{
	private String mName;
	
	public Method(){
		mName = "DEFAULT";
	}
	
	public Method(String name){
		mName = name;
	}
	
	public void showName(PageMethod activity){
		Toast.makeText(activity, "I Am "+mName, Toast.LENGTH_SHORT).show();
	}
}
